use lib "../BuildTool/";
use BuildTool;


use Test::More tests => 5;
use Test::Exception;
 
 BEGIN {

 	use_ok(BuildTool);
 }


subtest 'Test Case 7.1' => sub {
	plan tests => 3;
####################################################################################
#	
# Verifying that build_file_rev.txt was created correctly when it's not the first
#build
#
####################################################################################
	#BuildInfo hash
	my %build_file_rev = ( 'file1.c' => ['10'], #The revision of this file is smaller - will be deleted
						'file1.h' => ['10'], #The revision of this file is the same - will remain
						'file2.c' => ['5'], #This file doesn't exist in ITS hash - will be deleted
						'file3.c' => ['8'], #This file is the same - will remain
	);
	#ITS hash
	my %build_file_rev_changes = ( 'file4.c' => ['11','A'], #This file will be added
								'file1.c' => ['15','M'], #This file will replace the old one
								'file2.c' => ['6','D'], #This file will be deleted
	);
	my $code_dir = "trunk";
	my $first_build = "false";
	my $buildInfo = "buildInfo.txt";
	#Only needs the vcs_path when it's the first build
	my $vcs_path = "";
	#Compare list of files from polarion and from the buildInfo to initialize the %build_file_rev
	BuildTool->compRevLists(\%build_file_rev,\%build_file_rev_changes);
	system("mkdir -p trunk/code/build");
	BuildTool->genBuildInfo($code_dir,"trunk",$first_build,$first_base,$buildInfo,$vcs_path);
	is(-e "trunk/code/build/buildInfo.txt",1,"Verifying that the buildInfo.txt exists");
	open(FILE,"trunk/code/build/buildInfo.txt");
	my @lines = <FILE>;
	close FILE;
	is($lines[0],"file1.c file1.h file3.c file4.c \n","Verifying that the files are correct");
	is($lines[1],"15 10 8 11 ","Verifying that the revisions are correct");
	system("rm -rf trunk");
};

subtest 'Test Case 7.2' => sub {
	plan tests => 3;
####################################################################################
#	
# Verifying that the release history file was correctly updated when it's not the
# first build.
#
#
####################################################################################

	mkdir("tags");
	open(FILE, ">tags/release_history.txt");
	print FILE "cron1.3\ncron1.2";
	close FILE;
	#Setting parameters
	my $first_build = "false";
	my $release_history_location = "tags/release_history.txt";
	Build->updateRelHist($first_build,$release_history_location,"baseline","1.1");
	#Read the release_history.txt to check if the build tag is correct
	open(CMD,$release_history_location);
	my @base_number;
	my $line;
	while($line = <CMD>){
		chomp($line);
		push(@base_number,$line);
	} 
	close CMD;
	is($base_number[2],"cron1.2","Verifying build tag in release_history.txt");
	is($base_number[1],"cron1.3","Verifying build tag in release_history.txt");
	is($base_number[0],"base1.1","Verifying build tag in release_history.txt");
	system("rm -rf tags");


};

subtest 'Test Case 7.3/7.5' => sub {
	plan tests => 3;
####################################################################################
#	
# Verifying tagging build
#
#
####################################################################################
	#Using the branches folder because it's not possible to delete sub-folders in the tags folder. 
	my $vcs_path = "http://cad-test.medtronic.com/repo/crdm/device/firmware/tools/build_tool/branches/TS7";
	my $type = "baseline";
	my $number = "2.1";
	my $code_dir = "trunk/quals/main_fw";
	#Checkout the trunk directory
	VCS->coFirstBuild( "http://cad-test.medtronic.com/repo/crdm/device/firmware/tools/build_tool/trunk" );
	#Creating new file to test the svn add
	open(FILE,">$code_dir/code/added_file.txt");
	print FILE "ADDED FILE";
	close FILE;
	#Tagg files
	VCS->tagBuild($vcs_path,$type,$number,$code_dir);
	
	my $new_files_exists;
	#See if the new tag exists
	my $cmd = "svn ls http://cad-test.medtronic.com/repo/crdm/device/firmware/tools/build_tool/branches/TS7/tags/base2.1/code |";
	open(CMD, $cmd);
	while(my $line = <CMD>) {
		if($line =~ "added_file.txt"){
			$new_files_exists = "true";
		}
	}
	is($new_files_exists,"true","Verifying if the new file was added and tagged");
	system("svn remove http://cad-test.medtronic.com/repo/crdm/device/firmware/tools/build_tool/branches/TS7/tags/base2.1/ -m \"Test Case 7_3 - Verifying tagging build\"");
	BuildTool->removeFiles();
	#Check if the trunk directory and the svn_error.txt were deleted
	my $trunk_deleted = "true" if !(-d "trunk");
	my $svn_error_deleted = "true" if !(-e "svn_error.txt");
	is($trunk_deleted,"true","Vefifying that the trunk was deleted");
	is($svn_error_deleted,"true", "Vefifying that the svn_error.txt was deleted");
};

subtest 'Test Case 7.4/7.5' => sub {
	plan tests => 3;
####################################################################################
#	
# Verifying that tagging build fails when there's an existent build with the same
# tag
#
#
####################################################################################
	#Tag folder that contains the base2.1
	my $vcs_path = "http://cad-test.medtronic.com/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS7";
	my $type = "baseline";
	my $number = "2.1";
	my $code_dir = "trunk/quals/main_fw";
	#Checkout the trunk directory
	VCS->coFirstBuild( "http://cad-test.medtronic.com/repo/crdm/device/firmware/tools/build_tool/trunk" );
	dies_ok{VCS->tagBuild($vcs_path,$type,$number,$code_dir)} "Verifying that the build tool fails";
	#Check if the trunk directory and the svn_error.txt were deleted
	my $trunk_deleted = "true" if !(-d "trunk");
	my $svn_error_deleted = "true" if !(-e "svn_error.txt");
	is($trunk_deleted,"true","Vefifying that the trunk was deleted");
	is($svn_error_deleted,"true", "Vefifying that the svn_error.txt was deleted");
};